home *** CD-ROM | disk | FTP | other *** search
/ Turnbull China Bikeride / Turnbull China Bikeride - Disc 2.iso / STUTTGART / LANG / C / LIB / UNIXLIB37B / !UnixLib37 / src / unix / c / dup2 < prev    next >
Text File  |  1996-11-09  |  1KB  |  59 lines

  1. /****************************************************************************
  2.  *
  3.  * $Source: /unixb/home/unixlib/source/unixlib37/src/unix/c/RCS/dup2,v $
  4.  * $Date: 1996/10/30 21:59:01 $
  5.  * $Revision: 1.2 $
  6.  * $State: Rel $
  7.  * $Author: unixlib $
  8.  *
  9.  * $Log: dup2,v $
  10.  * Revision 1.2  1996/10/30 21:59:01  unixlib
  11.  * Massive changes made by Nick Burret and Peter Burwood.
  12.  *
  13.  * Revision 1.1  1996/04/19 21:35:27  simon
  14.  * Initial revision
  15.  *
  16.  ***************************************************************************/
  17.  
  18. static const char rcs_id[] = "$Id: dup2,v 1.2 1996/10/30 21:59:01 unixlib Rel $";
  19.  
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <unistd.h>
  23.  
  24. #include <sys/types.h>
  25. #include <sys/unix.h>
  26. #include <sys/os.h>
  27.  
  28. int
  29. dup2 (int fd1, int fd2)
  30. {
  31.   register struct file *f1, *f2;
  32.  
  33.   if (BADF (fd1) || (fd2 >= MAXFD) || (fd2 < 0))
  34.     {
  35.       errno = EBADF;
  36.       return (-1);
  37.     }
  38.  
  39.   if (fd1 == fd2)
  40.     return (0);
  41.  
  42.   f1 = __u->file + fd1;
  43.   f2 = __u->file + fd2;
  44.  
  45.   if (f2->dup)
  46.     close (fd2);
  47.  
  48.   memcpy (f2, f1, sizeof (struct file));
  49.  
  50.   f2->dup = f1;
  51.  
  52.   while (f1->dup != f2->dup)
  53.     f1 = f1->dup;
  54.  
  55.   f1->dup = f2;
  56.  
  57.   return (0);
  58. }
  59.